// pages/shopGoods/shopGoods.js
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classId: null,
        goodsClassify: [],
        goodsClassifyIndex: 'all',
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            classId: options.classId
        })
        this.getGoodsClassify()
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getGoodsList()
    },

    getGoodsClassify: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            if (that.data.classId != 'all') {
                that.setData({
                    goodsClassifyIndex: res.findIndex((e) => {
                        return e.class_id == that.data.classId
                    }),
                    goodsClassify: res
                })
            } else {
                that.setData({
                    goodsClassify: res
                })
            }
        }).catch(function(e) {})
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let goodsList = this.data.goodsList
        let data
        if (this.data.classId == 'all') {
            data = {
                activity: 785,
                store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
                is_sale: '1',
                is_goods: '0',
                client: 2,
                page: this.data.nowPage,
                limit: 10
            }
        } else {
            data = {
                activity: 785,
                store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
                class_id: this.data.classId,
                is_sale: '1',
                is_goods: '0',
                client: 2,
                page: this.data.nowPage,
                limit: 10
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (e.currentTarget.dataset.index == this.data.goodsClassifyIndex) {
            return
        }
        if (e.currentTarget.dataset.index == 'all') {
            this.setData({
                classId: 'all',
                goodsClassifyIndex: e.currentTarget.dataset.index,
                goodsList: [],
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
        } else {
            this.setData({
                classId: this.data.goodsClassify[e.currentTarget.dataset.index].class_id,
                goodsClassifyIndex: e.currentTarget.dataset.index,
                goodsList: [],
                nowPage: 0,
                isLoading: false,
                isAll: false
            })
        }
        this.getGoodsList()
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    }
})